package com.qumitech.visualnovel.controller.rest;


import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.pubdef.pubenum.GameTypeEnum;
import com.qumitech.visualnovel.service.oldgame.OldGameService;
import com.google.common.collect.ImmutableMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/game")
public class OldGameController {

    @Autowired
    OldGameService gameService;

    @GetMapping("/gameOptions")
    public BaseRsp getGameOptions() {
        Map<String, String> map = new HashMap<String, String>();
        Map<String, String> number_map = new HashMap<String, String>();  // 兼容 /location/block/console/plane 服务端接口

        for (GameTypeEnum gameType: GameTypeEnum.values()) {
            map.put(gameType.getFieldName(), gameType.getFieldNameCHN());
            number_map.put(gameType.getFiledNumber(), gameType.getFieldNameCHN());
        }
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", GameTypeEnum.values().length, "items", map, "number_items", number_map));
    }

    @GetMapping("/statistics")
    public BaseRsp gameStatistics(@RequestParam(value = "uid_list", required=false) List<String> uidList,
                                  @RequestParam(value = "gameType", required=false) String gameType,
                                  @RequestParam(value = "min_pound_diamonds", required=false) Integer startInput,
                                  @RequestParam(value = "max_pound_diamonds", required=false) Integer endInput,
                                  @RequestParam(value = "startTime", required=false) String startTime,
                                  @RequestParam(value = "endTime", required=false) String endTime,
                                  @RequestParam(value = "offset", required=false, defaultValue = "0") Integer offset,
                                  @RequestParam(value = "limit", required=false, defaultValue = "20") Integer limit,
                                  @RequestParam(value = "orderBy", required=false, defaultValue = "update_time") String orderBy,
                                  @RequestParam(value = "orderType", required=false, defaultValue = "DESC") String orderType) {
        List<HashMap<String,String>> mapList = new ArrayList<>();
        List<String> gameTypes = GameTypeEnum.listGameType();
        for (String game_type: gameTypes) {
            HashMap<String, String> map = new HashMap<>();
            map.put("gameType", game_type);
            map.put("tableName", String.join("_", "user_gaming", game_type, "statistics"));
            mapList.add(map);
        }
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", gameService.gameStatisticsListCount(uidList, gameType, startInput, endInput, startTime, endTime, mapList),
                "items", gameService.gameStatisticsList(uidList, gameType, startInput, endInput, startTime, endTime, offset, limit, orderBy, orderType, mapList)));
    }

    @GetMapping(value = "/nicknames")
    public BaseRsp listGameNicknames(@RequestParam(value = "accountId") Integer accountId,
                                     @RequestParam(value = "offset", required=false, defaultValue = "0") Integer offset,
                                     @RequestParam(value = "limit", required=false, defaultValue = "20") Integer limit,
                                     @RequestParam(value = "orderBy", required=false, defaultValue = "bind_time") String orderBy,
                                     @RequestParam(value = "orderType", required=false, defaultValue = "DESC") String orderType) {
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", gameService.countGamerNicknames(accountId),
                "items", gameService.listGamerNicknames(accountId, offset, limit, orderBy, orderType)));
    }

}
